package com.ciwong.epaper.modules.reciteWords.presenter;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.os.Build;
import android.util.Log;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.db.DBCallBack;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.reciteWords.dbreciteword.DBManagerAsyn;
import com.ciwong.libs.audio.play.AudioPlayer;
import com.ciwong.libs.audio.play.OnPlayListener;
import com.ciwong.mobilelib.utils.ToastUtil;

public class ReciteWordDetailPresenter {

    private final String TAG = "ReciteWordDetailPresent";
    private final Context mContext;
    private final IReciteWordDetailactivity iReciteWordDetailactivity;

    public ReciteWordDetailPresenter(Context context, IReciteWordDetailactivity iReciteWordDetailactivity) {
        this.mContext = context;
        this.iReciteWordDetailactivity = iReciteWordDetailactivity;
    }

    public void queryDataFromDB(String bookId,String did) {

        DBManagerAsyn.getInstance().queryDIdWordDetailAsyn(bookId + "", did + "", new DBCallBack() {
            @Override
            public void dbResult(Object data) {
                WordDetail wordDetail = (WordDetail)data;
                if (iReciteWordDetailactivity!=null)
                    iReciteWordDetailactivity.onQueryReult(wordDetail);
            }
        });


    }

    public void startPlay(String url) {
        try {
            AudioPlayer.getInstance().setOnPlayListener(listener).play(url);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && EApplication.getInstance().mPlayerspeed !=1.0f) {
                MediaPlayer mMediaPlayer = AudioPlayer.getInstance().getMediaPlayer();
                if (mMediaPlayer != null) {
                    try {
                        PlaybackParams params = mMediaPlayer.getPlaybackParams().setSpeed(EApplication.getInstance().mPlayerspeed);
                        if (params != null) {
                            mMediaPlayer.setPlaybackParams(params);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void stopPlay() {
        AudioPlayer.getInstance().stop();
    }


    private OnPlayListener listener = new OnPlayListener() {
        @Override
        public void stop(Object mp3) {
            if (iReciteWordDetailactivity != null) {
                iReciteWordDetailactivity.onStopPlay(mp3);
            }

        }

        @Override
        public void onPlayStart(Object mp3) {
            if (iReciteWordDetailactivity != null) {
                iReciteWordDetailactivity.onStartPlay(mp3);
            }
        }

        @Override
        public void onReadPlayer(long duration, Object mp3) {
        }

        @Override
        public void onError(int errorCode, final Object tag) {
            Log.e(TAG, "播放音频失败 onError: " + errorCode);
            ToastUtil.INSTANCE.toastCenter(mContext, "播放音频失败", R.mipmap.ic_error);
            if (iReciteWordDetailactivity != null) {
                iReciteWordDetailactivity.onPlayError(errorCode,tag);
            }
        }

        @Override
        public void onBuffer(Object mp3, long progress, long total) {
        }

        @Override
        public void onPlaying(long arg0, long arg1) {
        }

    };
}
